from django.urls import reverse
from django.conf import settings
from ninja_extra.exceptions import APIException

from itsdangerous import URLSafeTimedSerializer as URLSTS


# def get_absolute_url_without_request(token):
#     return reverse('userinfo', args=[token])

class Token:

    def __init__(self):
        pass

    def generate_token(self, user):
        """通过itsdangerous生成Token，用于邮箱激活"""
        serializer = URLSTS(settings.SECRET_KEY, salt=settings.SECRET_KEY, )
        data = {
            'uid': user.id,
            'email': user.email,
        }
        return serializer.dumps(data)

    # def generate_url(self, request):
    #     """生成链接"""
    #     # 这种方法返回的域名包括了端口号
    #     domain_name_port = request.get_host()
    #     # 这种方法返回的域名剔除了端口号
    #     domain_name = request.get_host().split(':')[0]
    #     # 该方法返回的URL包括了协议、域名、端口号（如果有）和路径
    #     full_url = request.build_absolute_uri()
    #     return f'{full_url}' +'?token={token}'
